package Solution.problem049;

import com.sun.tools.javac.util.ArrayUtils;
import sun.security.provider.MD5;

import java.util.*;

/**
 * @program Leetcode
 * @description:
 * @author: lishangsheng
 * @create: 2019/04/01 12:01
 */
public class Solution {
    public static List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> results=new ArrayList<List<String>>();
        Map<String,List<String>> hashMap=new HashMap<String, List<String>>();
        for(String s:strs){
            char[] chars=s.toCharArray();
            Arrays.sort(chars);
            StringBuilder stringBuilder=new StringBuilder();
            for(char c:chars){
                stringBuilder.append(c);
            }
            if(hashMap.containsKey(stringBuilder.toString())){
                List<String> list=hashMap.get(stringBuilder.toString());
                list.add(s);
            }else{
                List<String> list=new ArrayList<String>();
                list.add(s);
                hashMap.put(stringBuilder.toString(),list);
            }
        }
        for (Map.Entry<String, List<String>> entry : hashMap.entrySet()) {
            results.add(entry.getValue());
        }

        return results;
    }

    public static void main(String[] args){
        String[] array=new String[]{"eat", "tea", "tan", "ate", "nat", "bat"};
        List<List<String>> result=groupAnagrams(array);
        System.out.println(result);
    }

}
